import {
  Notify
} from 'vant';
import axios from 'axios';
import jsBridge from '../libs/jsBridge';


//定义
export const prefix = '/api'
export let _platform_id = "13";

//设置请求方法
export function setAxiosParam() {
  return jsBridge.getPlatForm_id().then(res => {
    _platform_id = res
  }).then(() => {
    axios.interceptors.request.use(
      config => {
        // 设置参数格式
        if (!config.headers['Content-Type']) {
          config.headers = {
            'Content-Type': 'application/json',
            'FZM-PLATFORM-ID': _platform_id,
          }
        }
        // 鉴权参数设置
        if (config.method === 'get') {
          //get请求下 参数在params中，其他请求在data中
          config.params = config.params || {};
          let json = JSON.parse(JSON.stringify(config.params));
          //一些参数处理
        } else {
          config.data = config.data || {};
          //一些参数处理
        }
        return config;
      },
      err => {
        return Promise.reject(err);
      }
    );
    //http response 拦截器
    axios.interceptors.response.use(
      response => {
        return response;
      },
      error => {
        return Promise.reject(error)
      }
    );
  })
}


/**
 * 封装get方法
 * @param url
 * @param params
 * @returns {Promise}
 */
export function fetch(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
        params: params,
      }, ).then(response => {
        if (response.data.code === 0) {
          resolve(response.data.data);
        } else if (url.indexOf("apply-detail") > -1 && response.data.data) {
          response.data.data.msg = response.data.msg
          resolve(response.data.data)
        } else {
          //错误处理
          Notify(response.data.msg)
          reject(response.data)
        }
      })
      .catch(err => {
        reject(err);
        let message = '请求失败！请检查网络';
        //错误返回
        if (err.response) message = err.response.data.msg;
        Notify(message)
      })
  })
}

/**
 * 封装post方法
 * @param url
 * @param params
 * @returns {Promise}
 */

export function post(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.post(url, data, ).then((response) => {
      if (response.data.code === 0) {
        resolve(response.data.data);
      } else if (response.data.result != "" && response.data.result != undefined) {
        resolve(response.data.result)
      } else {
        //错误处理
        if (typeof (response.data.msg) == 'object') {
          if (url.indexOf('apply') > 1) Notify('提交失败');
        } else if (response.data.error != null || response.data.error != undefined) {
          //区块链错误信息
          reject(response.data.error)
          return;
        } else {
          Notify(response.data.msg)
        }
        reject(response.data)
      }
    }).catch((err) => {
      let message = '请求失败！请检查网络';
      //错误返回
      if (err.response) {
        message = err.response.data.msg;
        Notify(message);
        reject(err);
      } else {
        Notify(message);
      }
    })
  })
}

/**
 * 计算精度0.001乘法
 * @param arg1
 * @param arg2
 * @returns {number}
 * @constructor
 */
export function Multiplication(arg1, arg2) {
  var r1 = arg1.toString(),
    r2 = arg2.toString(),
    m, resultVal, d = arguments[2];
  m = (r1.split(".")[1] ? r1.split(".")[1].length : 0) + (r2.split(".")[1] ? r2.split(".")[1].length : 0);
  resultVal = Number(r1.replace(".", "")) * Number(r2.replace(".", "")) / Math.pow(10, m);
  return typeof d !== "number" ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)));
}